package com.livemeter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

public class QuestionsActivity extends Activity 
{
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.questions);

		LIM_Engine lim = LIM_Engine.getInstance();
		lim.removeEventListener(m_lim_event_listener);
		lim.addEventListener(m_lim_event_listener);
		
		Button askButton = (Button) findViewById(R.id.AskQuestionButton);
		askButton.setOnClickListener( new OnClickListener() {
        	public void onClick(View v)
        	{
        		LIM_Engine lim = LIM_Engine.getInstance();
        		EditText questionEdit = (EditText) QuestionsActivity.this.findViewById(R.id.questionText);
        		String questionStr = questionEdit.getText().toString();
				lim.client_AskQuestion(questionStr);
				questionEdit.setText("");
        	}
        } );
    }
    

    LIM_Event_Listener m_lim_event_listener = new LIM_Event_Listener() {
		@Override
		public void meterValueChanged(LIM_MeterValueEvent evt) {}
		@Override
		public void pollOpened(LIM_PollEvent evt) {}
		@Override
		public void pollUpdated(LIM_PollEvent evt) {}
		@Override
		public void pollClosed(LIM_PollEvent evt) {}
		@Override
		public void meterOpened(LIM_MeterEvent evt) {}
		@Override
		public void meterClosed(LIM_MeterEvent evt) {}
		@Override
		public void questionsChanged() 
		{
			QuestionsActivity.this.runOnUiThread(new Runnable() {
				public void run() {QuestionsActivity.this.loadQuestions();}
			}); 
		}
		@Override
		public void topicStamp(LIM_TopicStampEvent evt) {}
		@Override
		public void setupTimeout() {}
		@Override
		public void tick() {}
    };

    @Override
    protected void onResume() {
    	super.onResume();
		// load available questions with their rating
    	loadQuestions();
    }
    
    protected void loadQuestions()
    {
    	LinearLayout layout = (LinearLayout) this.findViewById(R.id.questionList);
    	layout.removeAllViews();
    	
    	LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    	lp.setMargins(0, 0, 0, 0);
    	lp.weight = 1;

    	LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    	
    	for (Question question : Question.getQuestions())
    	{
    		LinearLayout llv = new LinearLayout(this);
    		llv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    		llv.setOrientation(LinearLayout.VERTICAL);

    		TextView questionText = new TextView(this);
    		questionText.setText(question.question);
    		questionText.setTextSize(20);
    		questionText.setLayoutParams(lp);
    		llv.addView(questionText);

    		LinearLayout ll = new LinearLayout(this);
    		ll.setLayoutParams(lp);
    		ll.setOrientation(LinearLayout.HORIZONTAL);
    		
    		// R.style.RatingBarCustom
			// android.R.attr.ratingBarStyleSmall
    		final String questionStr = question.question;
    		RatingBar rating = new RatingBar(this,null);
    		rating.setLayoutParams(lp2);
    		rating.setNumStars(5);
    		if (question.vote > 0.0f)
    		{
    			rating.setRating(question.voteAvg);
    		} else {
    			rating.setRating(0);
    		}
    		rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
				@Override
				public void onRatingChanged(RatingBar ratingBar,float rating, boolean fromUser) {
					// Send Vote
					Question q = Question.findQuestion(questionStr);
					q.vote = rating;
					LIM_Engine lim = LIM_Engine.getInstance();
					lim.client_QuestionVote(q);
				}});
    		ll.addView(rating);

    		if (question.vote > 0.0f)
    		{
	    		TextView voteCountText = new TextView(this);
	    		voteCountText.setText("("+String.valueOf(question.voteCount)+")");
	    		voteCountText.setTextSize(20);
	    		ll.addView(voteCountText);
    		}
    		
    		llv.addView(ll);
    		
    	    // <View android:layout_height="1dp" android:id="@+id/View09" android:layout_weight="1" android:layout_width="fill_parent" android:background="@drawable/black_white_gradient"></View>
        	LinearLayout.LayoutParams lpdiv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        	lpdiv.setMargins(0, 0, 0, 0);
        	lpdiv.height = 5;

        	View divider = new View(this); 
    		divider.setBackgroundResource(R.drawable.black_white_gradient);
    		divider.setLayoutParams(lpdiv);

        	layout.addView(llv);
    		layout.addView(divider);
    	}
    
    }
}
